package com.zmn.plat.admin.controller.orderconfig.orderstock.sp.warning;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.orderconfig.orderstock.sp.SpWarningConfig;

/**
 * 服务商库存预警配置
 *
 * @author xiangjunming
 * @since 2020/07/22 10:43:04
 * @since
 */
@Controller
@RequestMapping("/plat/sp/warning/config/")
public class SpWarningConfigModifyController extends SpWarningConfigBaseController {

    /**
     * logger 描述此字段
     */
    private Logger logger = LoggerFactory.getLogger(SpWarningConfigModifyController.class);

    /**
     * 保存批量设置
     *
     * @param list
     *            the list
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 2020/07/22 10:43:04
     */
    @RequestMapping(value = "saveBatchSet")
    @ResponseBody
    public ResponseDTO saveBatchSet(@RequestBody List<SpWarningConfig> list) {
        try {
            for (SpWarningConfig item : list) {
                if (item.getProportion() < 1 || item.getProportion() > 100) {
                    return ResponseDTO.fail("库存预警比例1~100!");
                }
            } ;
            String realName = getMcStaffRealName();
            spWarningConfigBService.saveBatchSet(list, realName);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 修改节假日系数
     *
     * @param configId
     *            the config id
     * @param value
     *            the value
     * @return the result dto
     */
    @RequestMapping(value = "updateCoefficient")
    @ResponseBody
    public ResponseDTO updateCoefficient(Integer configId, Double value) {
        try {
            SpWarningConfig spWarningConfig = new SpWarningConfig();
            spWarningConfig.setConfigId(configId);
            spWarningConfig.setCoefficient(value);
            spWarningConfig.setUpdater(getMcStaffRealName());
            spWarningConfig.setUpdateTime(DateUtil.getNow());
            spWarningConfigService.updateByKey(spWarningConfig);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 修改库存量
     *
     * @param configId
     *            the config id
     * @param configId
     *            the value
     * @return the result dto
     */
    @RequestMapping(value = "updateStock")
    @ResponseBody
    public ResponseDTO updateStock(Integer configId, Integer stock, Integer proportion) {
        try {
            if (stock == null) {
                stock = -1;
            }
            if (proportion < 1 || proportion > 100) {
                return ResponseDTO.fail("库存预警比例1~100!");
            }
            SpWarningConfig spWarningConfig = new SpWarningConfig();
            spWarningConfig.setConfigId(configId);
            spWarningConfig.setStock(stock);
            spWarningConfig.setProportion(proportion);
            spWarningConfig.setUpdater(getMcStaffRealName());
            spWarningConfig.setUpdateTime(DateUtil.getNow());
            spWarningConfigService.updateByKey(spWarningConfig);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }
}
